package com.dexilaonian.adpter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dexilaonian.R;
import com.dexilaonian.activity.MyStoreOrderAcitivity;
import com.dexilaonian.activity.PayingActivity;
import com.dexilaonian.bean.SaleOrderBean;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.ImageLoader;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class MyStoreOrderAdapter extends BaseAdapter{
    private final Context mContext;
    private final List<SaleOrderBean> mStoreList;

    private boolean fadeTips = false; // 变量，是否隐藏了底部的提示
    private Handler mHandler = new Handler(Looper.getMainLooper()); //获取主线程的Handler
    private String orderId;

    public MyStoreOrderAdapter(Context context, List<SaleOrderBean> storelist) {
        mContext = context;
        mStoreList = storelist;
        Log.e("TTTT", storelist.size()+"");
    }

    @Override
    public int getCount() {
        return mStoreList == null? 0 :mStoreList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_store, null);
            viewHolder.linearLayout = (LinearLayout) convertView.findViewById(R.id.ll_store_detail);
            viewHolder.orderNum = (TextView) convertView.findViewById(R.id.order_number);
            viewHolder.ispay = (TextView) convertView.findViewById(R.id.isPay);
            viewHolder.store_order_pic = (ImageView) convertView.findViewById(R.id.item_store_order);
            viewHolder.storeTitle = (TextView) convertView.findViewById(R.id.item_store_title);
            viewHolder.storetext = (TextView) convertView.findViewById(R.id.item_store_text);
            viewHolder.storePrice = (TextView) convertView.findViewById(R.id.item_store_price);
            viewHolder.store_pay = (TextView) convertView.findViewById(R.id.item_store_pay);
            viewHolder.store_cancle = (TextView) convertView.findViewById(R.id.item_store_cancle);
            
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String orderPrice = mStoreList.get(position).getOrderPrice();
        final String refNo = mStoreList.get(position).getRefNo();
        if (!TextUtils.isEmpty(refNo)) {
            viewHolder.orderNum.setText("订单号:"+refNo);
        }
        final String status = mStoreList.get(position).getStatus();
        if (!TextUtils.isEmpty(status)) {
            viewHolder.ispay.setText(status);
        }
        if (!TextUtils.isEmpty(orderPrice)) {
            viewHolder.storePrice.setText(orderPrice);
        }
      List<SaleOrderBean.ProductDetailBean> productDetail = mStoreList.get(position).getProductDetail();
        if (productDetail != null) {
            for (int i = 0; i < productDetail.size(); i++) {
                String picUrl = productDetail.get(i).getPicUrl();
                ImageLoader.display(mContext,picUrl,viewHolder.store_order_pic);
                int quantity = productDetail.get(i).getQuantity();

                double price = productDetail.get(i).getPrice();
                int productId = productDetail.get(i).getProductId();
                String productName = productDetail.get(i).getProductName();
                if (!TextUtils.isEmpty(productName)) {
                    viewHolder.storeTitle.setText(productName);
                }
                String pspec = productDetail.get(i).getPspec();
                if (!TextUtils.isEmpty(pspec)) {
                    viewHolder.storetext.setText(pspec);
                }
            }
        }

        viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PayingActivity.class);
                if (!TextUtils.isEmpty(orderId)) {
                    intent.putExtra("OrderId", orderId);
                }
                mContext.startActivity(intent);
            }
        });
        viewHolder.store_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PayingActivity.class);
                if (!TextUtils.isEmpty(orderId)) {
                    intent.putExtra("OrderId", orderId);
                }
                mContext.startActivity(intent);
            }
        });
        if (!"".equals(orderId)) {
        viewHolder.store_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item_cancle_url = Constant.base_url + "/sale_order/cancel.do";
                Map<String, String> item_cancle_map = new HashMap<String, String>();
                if (!TextUtils.isEmpty(orderId)) {
                    item_cancle_map.put("OrderId", orderId);
                }
                Okhttp.postString(true, item_cancle_url, item_cancle_map, new Okhttp.CallBac() {
                    @Override
                    public void onError(Call call, Exception e, String state, int id) {
                        Toast.makeText(mContext,state,Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int status = Integer.parseInt(jsonObject.optString("status"));
                            if (status == 0) {
                                Toast.makeText(mContext,"取消成功",Toast.LENGTH_SHORT).show();
                                mStoreList.remove(position);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        notifyDataSetChanged();
                    }

                    @Override
                    public void onNoNetwork(String state) {
                        Toast.makeText(mContext,state,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        }
        return convertView;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    class ViewHolder{
        TextView orderNum;
        TextView ispay;
        ImageView store_order_pic;
        TextView storeTitle;
        TextView storetext;
        TextView storePrice;
        TextView store_pay;
        TextView store_cancle;
        LinearLayout linearLayout;
    }
}
